package jp.co.sopia;

import android.app.Activity;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xmlpull.v1.XmlPullParserException;

import jp.co.sopia.bean.NoteBean;
import jp.co.sopia.custom.ListNoteAdapter;
import jp.co.sopia.util.UtilFunctions;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {
	private static final int EDIT_NOTE = Menu.FIRST;
	private static final int NEW_NOTE = Menu.FIRST + 1;
	private static final int DEL_NOTE = Menu.FIRST + 2;
	private ArrayList<NoteBean> arrayNotes;
	private ListNoteAdapter arrayAdapter;
	
	private Intent mainTabBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		arrayNotes = new ArrayList<NoteBean>();
		arrayAdapter = new ListNoteAdapter(this, R.layout.list, arrayNotes);
		mainTabBar = new Intent(this, MainTabBar.class);
		
		loadData();
		final ListView list = (ListView) findViewById(R.id.listNotes);
		list.setAdapter(arrayAdapter);
		
	}
	
	/**
	 * Read data from xml
	 */
	private void loadData() {
		Resources res = this.getResources();
		InputStream is = res.openRawResource(R.raw.data);
		DocumentBuilder builder;
		Document doc;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			doc = builder.parse(is);
			NodeList listNotes =  doc.getElementsByTagName("Note");
			for (int i = 0; i < listNotes.getLength(); i++) {
				NoteBean note = new NoteBean();
				Element item = (Element)listNotes.item(i);
				note.setName(item.getAttribute("name"));
				String imp = UtilFunctions.getValueChildFromElement(item, "importance");
				if("low".equals(imp))
					note.setImportance(R.id.low);
				if("high".equals(imp))
					note.setImportance(R.id.high);
				if("medium".equals(imp))
					note.setImportance(R.id.medium);
				note.setChecked(false);
				note.setContent(UtilFunctions.getValueChildFromElement(item, "summary"));
				note.setDeadline(UtilFunctions.getValueChildFromElement(item, "deadline"));
				note.setRemind(UtilFunctions.getValueChildFromElement(item, "remind"));
				arrayNotes.add(note);
			}
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, EDIT_NOTE, 0,"Edit").setIcon(android.R.drawable.ic_menu_edit);
		menu.add(0, NEW_NOTE, 0,"New").setIcon(android.R.drawable.ic_menu_add);
		menu.add(0, DEL_NOTE, 0,"Delete").setIcon(android.R.drawable.ic_delete);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
			case EDIT_NOTE : {
				NoteBean noteSelected = getNoteSelected();
				if(noteSelected != null) {
					Bundle sendBundle = new Bundle();
					sendBundle.putSerializable("note", noteSelected);
					mainTabBar.putExtras(sendBundle);
					startActivity(mainTabBar);
					arrayAdapter.notifyDataSetChanged();
				}
				break;
			}
			case NEW_NOTE : {
				mainTabBar = new Intent(this, MainTabBar.class);
				startActivity(mainTabBar);
				arrayAdapter.notifyDataSetChanged();
				break;
			}
			case DEL_NOTE : {
				AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
				alertDlg.setTitle("Confirm");
				alertDlg.setIcon(android.R.drawable.ic_dialog_alert);
				alertDlg.setMessage("Are you sure???");
				alertDlg.setPositiveButton("Yes", new OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						deleteNotes();
					}
				});
				alertDlg.setNegativeButton("No", new OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.cancel();
					}
				});
				//Show confirm
				alertDlg.show();
				break;
			}
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * Checks note is selected
	 * @return
	 */
	private NoteBean getNoteSelected() {
		for(NoteBean note : arrayNotes) {
			if(note.isChecked()) {
				return note;
			}
		}
		return null;
	}
	
	private void deleteNotes() {
		for(NoteBean note : arrayNotes) {
			if(note.isChecked()) {
				arrayNotes.remove(note);
				arrayAdapter.notifyDataSetChanged();
				if(arrayNotes.size() <= 0) break;
				continue;
			}
		}
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
	}
}
